// run: $exec < input
// opt: 0
// flag: -g
#include <bits/stdc++.h>

long long const maxn = 100;
long long a[maxn], prime[maxn];
long long ta[maxn], tp[maxn];
long long n;
long long x, y;

void extended_gcd(long long a, long long b, long long & x, long long & y)
{
	if (!b) { x = 1; y = 0; }
	else {
		extended_gcd(b, a % b, y, x);
		y -= x * (a/b);
	}
}

long long inverse(long long t, long long p)
{
	long long x, y;
	extended_gcd(t, p, x, y);
	return (x % p + p) % p;
}

long long calc(long long a, long long M, unsigned long long len)
{
	long long k = (7 - (a % 7)) * inverse(M % 7, 7);
	k = (k % 7) + 7;
	k %= 7;
	a += k * M;
	if ((unsigned long long)a > len) return 0;
	unsigned long long mm = M;
	mm *= 7;
	return ((len - (unsigned long long)a) / mm) + 1;
}

long long crt(long long a[], long long m[], long long n)
{
	long long M = 1;
	long long ans = 0;
	for(long long i=1; i<=n; i++)
		M *= (long long)m[i];
	for(long long i=1; i<=n; i++) {
		long long x, y;
		long long Mi = M / m[i];
		extended_gcd(Mi, m[i], x, y);
		long long tmp = (((x * a[i]) % M) * Mi) % M;
		ans = (ans + tmp) % M;
	}
	ans = (ans % M) + M;
	ans %= M;
	return calc(ans, M, y) - calc(ans, M, x - 1);
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	long long T; std::cin >> T;
	for (long long ti = 1; ti <= T; ti++) {
		std::cout << "Case #" << ti << ": ";
		std::cin >> n >> x >> y;
		for (long long i = 1; i <= n; i++) std::cin >> prime[i] >> a[i];

		long long ans = 0;
		for (long long i = 0; i < (1 << n); i++) {
			long long len = 0, count = 0;
			for (long long j = 0; j < n; j++) {
				if (!((i >> j) & 1)) continue;
				ta[++len] = a[j + 1];
				tp[len] = prime[j + 1];
				count++;
			}
			if (count & 1) ans -= crt(ta, tp, len);
			else ans += crt(ta, tp, len);
		}
		std::cout << ans << "\n";
	}
}

